package main

import (
	"flag"
	"fmt"
	"os"

	"zerorpc/app/ogzero/rpc/demosvc/demosvc"
	"zerorpc/app/ogzero/rpc/demosvc/internal/config"
	demoServer "zerorpc/app/ogzero/rpc/demosvc/internal/server/demo"
	"zerorpc/app/ogzero/rpc/demosvc/internal/svc"
	"zerorpc/pkg/interceptor"

	"github.com/zeromicro/go-zero/core/logx"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

var pwd, _ = os.Getwd()
var configFile = flag.String("f", pwd+"/app/ogzero/rpc/demosvc/etc/demo.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	ctx := svc.NewServiceContext(c)

	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		demosvc.RegisterDemoServer(grpcServer, demoServer.NewDemoServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	defer s.Stop()

	// 自定义参数校验拦截器
	s.AddUnaryInterceptors(interceptor.ValidatorInterceptor)
	// 自定义错误转码拦截器
	s.AddUnaryInterceptors(interceptor.LoggerInterceptor)

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)

	logx.DisableStat()
	s.Start()
}
